package com.easyattendance.util;

import org.hibernate.Session;
import java.sql.*;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

	private static final SessionFactory sessionFactory;
	
	/* static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost/";
    
    //  Database credentials
    static final String USER = "root";
    static final String PASS = "root";  */

	static {
		 try {
			// DatabaseCreation();
			 sessionFactory = new AnnotationConfiguration().configure("com/easyattendance/resources/hibernate.cfg.xml").buildSessionFactory();
		 } catch (Throwable ex) {
			 System.err.println("Initial SessionFactory creation failed." + ex);
			 throw new ExceptionInInitializerError(ex);
		 }
	}

	public static Session openSession() {
		 return sessionFactory.openSession();
	}
	
	/* public static void DatabaseCreation()
    {
    	  Connection conn = null;
    	   Statement stmt = null;
    	   try{
    	     
    	      Class.forName("com.mysql.jdbc.Driver");

    	      // Open a connection
    	      System.out.println("Connecting to database...");
    	      conn = DriverManager.getConnection(DB_URL, USER, PASS);

    	      // Execute a query
    	      System.out.println("Creating database...");
    	      stmt = conn.createStatement();
    	      
    	      String sql = "CREATE DATABASE IF NOT EXISTS NEW_EASY_DB";
    	      stmt.executeUpdate(sql);
    	      System.out.println("Database created successfully...");
    	   }catch(SQLException se){
    	      //Handle errors for JDBC
    	      se.printStackTrace();
    	   }catch(Exception e){
    	      //Handle errors for Class.forName
    	      e.printStackTrace();
    	   }finally{
    	    
    	      try{
    	         if(stmt!=null)
    	            stmt.close();
    	      }catch(SQLException se2){
    	      }
    	      try{
    	         if(conn!=null)
    	            conn.close();
    	      }catch(SQLException se){
    	         se.printStackTrace();
    	      }
    	   }
    }  */
	
}
